package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private SetmealDao setmealDao;

    @Override
    public Result submit(Map map) {
        //判断预约设置是否设置预约人数
        String orderDate = (String)map.get("orderDate");
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);

        if(orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //查看当前日期是否已预约满
        //预约人数
        int number = orderSetting.getNumber();

        //可预约人数
        int reservations = orderSetting.getReservations();

        if(number <= reservations) {
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //查看同一天是否预约两次
        //根据手机号判断是否是会员
        String telephone = (String)map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);

        if(member != null) {
            //根据日期查看预约设置是否有重复的日期
            List<Order> list = orderDao.findByDate(member.getId(),orderDate);

            if(list != null && list.size() > 0) {
                return new Result(false,MessageConstant.HAS_ORDERED);
            }

        } else {

            //注册会员
            member = new Member();
            member.setName((String) map.get("name"));
            member.setIdCard((String) map.get("idCard"));
            member.setRegTime(new Date());
            member.setSex((String) map.get("sex"));
            member.setPhoneNumber((String) map.get("telephone"));
            memberDao.add(member);

            //预约
            orderSetting.setReservations(reservations+1);
            orderSettingDao.editReservationsByOrderDate(orderSetting);
        }

        //保存信息到预约表中
        Order order = new Order();
        order.setMemberId(member.getId());
        try {
            order.setOrderDate(DateUtils.parseString2Date(orderDate));
        } catch (Exception e) {
            e.printStackTrace();
        }
        order.setOrderType(order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) {

        //创建一个map，存储返回的数据
        Map map = new HashMap();

        Order order = orderDao.findById(id);
        //添加体检日期
        try {
            String dateStr = DateUtils.parseDate2String(order.getOrderDate(),"yyyy/MM/dd");
            map.put("orderDate",dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //添加体检类型
        map.put("orderType",order.getOrderType());
        //查询体检人
        Member member = memberDao.findByMemberId(order.getMemberId());
        map.put("member",member.getName());
        //查询体检套餐名字
        Setmeal setmeal = setmealDao.findById(order.getSetmealId());
        map.put("setmeal",setmeal.getName());
        return map;
    }

    @Override
    public List<Map<String,Object>> getSetmealReport() {
        return orderDao.getSetmealReport();
    }
}
